package com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.InvoiceDetailsModel;
import com.yikaiye.android.yikaiye.model.InvoiceRecordsModel;
import com.yikaiye.android.yikaiye.model.ServiceRecordsModel;
import com.yikaiye.android.yikaiye.model.company.CompanyIntroModel;
import com.yikaiye.android.yikaiye.model.company.GsBaseInfoModel;
import com.yikaiye.android.yikaiye.model.company.GsChangeRecordDataModel;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompanyDetailsViewModel extends BaseViewModel {

    /**
     * 企业详情信息
     */
    public MutableLiveData<CompanyIntroModel> companyDetailsModelMutableLiveData = new MutableLiveData<>();
    /**
     * 企业工商信息
     */
    public MutableLiveData<GsBaseInfoModel> gsBaseInfoModelMutableLiveData = new MutableLiveData<>();
    /**
     * 企业工商错误信息
     */
    public MutableLiveData<String> errorBaseInfoMutableLiveData = new MutableLiveData<>();
    /**
     * 工商变更信息
     */
    public MutableLiveData<GsChangeRecordDataModel> gsChangeRecordDataModelMutableLiveData = new MutableLiveData<>();
    /**
     * 工商变更信息错误信息
     */
    public MutableLiveData<String> errorGsChangeMutableLiveData = new MutableLiveData<>();
    /**
     * 发票列表数据
     */
    public MutableLiveData<InvoiceRecordsModel> invoiceRecordsModelMutableLiveData = new MutableLiveData<>();
    /**
     * 发票详情数据
     */
    public MutableLiveData<InvoiceDetailsModel> invoiceDetailsModelMutableLiveData = new MutableLiveData<>();
    /**
     * 企业关联服务
     */
    public MutableLiveData<List<ServiceRecordsModel.ServiceModel>> serviceProgressListModelMutableLiveData = new MutableLiveData<>();


    /**
     * 查找企业详情
     *
     * @param creditNo 企业信用代码
     */
    public void getCompanyDetail(String creditNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("creditNo", creditNo);
        addSubscribe(mRetrofitHelper.getCompanyDetail(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CompanyIntroModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyIntroModel> baseModel) {
                if (baseModel.getCode() == Constant.SUCCESS) {
                    companyDetailsModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 工商信息基本信息
     */
    public void getGsBaseInfo(String eid) {
        addSubscribe(mRetrofitHelper.getGsBaseInfo(eid).subscribeWith(new BaseNetCallback<GsBaseInfoModel>() {

            @Override
            public void onSuccess(BaseModel<GsBaseInfoModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        gsBaseInfoModelMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    errorBaseInfoMutableLiveData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                errorBaseInfoMutableLiveData.setValue("error");
            }
        }));
    }

    /**
     * 工商变更记录
     */
    public void getGsChangeRecord(String eid, String creditNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", 1);
        map.put("pageSize", 50);
        map.put("creditNo", creditNo);
        map.put("eid", eid);
        addSubscribe(mRetrofitHelper.getGsChangeRecord(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<GsChangeRecordDataModel>() {

            @Override
            public void onSuccess(BaseModel<GsChangeRecordDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        gsChangeRecordDataModelMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    errorGsChangeMutableLiveData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                errorGsChangeMutableLiveData.setValue("error");
            }
        }));
    }

    /**
     * 发票列表
     *
     * @param creditNo 企业信用代码
     */
    public void getInvoiceList(String creditNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("creditNo", creditNo);
        addSubscribe(mRetrofitHelper.getInvoiceList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<InvoiceRecordsModel>() {

            @Override
            public void onSuccess(BaseModel<InvoiceRecordsModel> baseModel) {
                if (baseModel.getCode() == Constant.SUCCESS) {
                    invoiceRecordsModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }

    /**
     * 发票详情
     *
     * @param id 发票id
     */
    public void getInvoiceDetails(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("invoiceId", id);
        addSubscribe(mRetrofitHelper.getInvoiceDetails(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<InvoiceDetailsModel>() {

            @Override
            public void onSuccess(BaseModel<InvoiceDetailsModel> baseModel) {
                if (baseModel.getCode() == Constant.SUCCESS) {
                    invoiceDetailsModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }


    /**
     * 查询企业关联服务
     *
     * @param creditNo 企业信用代码
     */
    public void getServiceByCreditNo(String creditNo) {
        addSubscribe(mRetrofitHelper.getServiceByCreditNo(creditNo).subscribeWith(new BaseNetCallback<List<ServiceRecordsModel.ServiceModel>>() {

            @Override
            public void onSuccess(BaseModel<List<ServiceRecordsModel.ServiceModel>> baseModel) {
                if (baseModel.getCode() == Constant.SUCCESS) {
                    serviceProgressListModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }
        }));
    }
}
